package edu.intec.matsab.form;

import edu.intec.matsab.dao.AsignaturaDAO;
import edu.intec.matsab.dao.CarreraDAO;
import edu.intec.matsab.dao.PensumDAO;
import edu.intec.matsab.persistence.Asignatura;
import edu.intec.matsab.persistence.Carrera;
import edu.intec.matsab.persistence.Pensum;
import edu.intec.matsab.persistence.PensumPK;
import edu.intec.matsab.xml.AsignaturaXML;
import edu.intec.matsab.xml.PensumXML;
import edu.intec.matsab.xml.TrimestreXML;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.el.ELContext;
import javax.el.ValueExpression;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author matilde
 */
public class ManejoPensumBean {

    private PensumXML pensumXml;
    private Pensum pensum;
    private boolean success;
    private Carrera carrera;
    private List<SelectItem> carrerasItems;
    private List<Asignatura> asignaturaItems;

    public ManejoPensumBean() {
        TrimestreXML trimestreXml = new TrimestreXML();
        trimestreXml.setListAsignaturas(new ArrayList<AsignaturaXML>());
        pensumXml = new PensumXML(new ArrayList<TrimestreXML>());
        pensumXml.getListTrimestres().add(trimestreXml);
        
        carrera = new Carrera();
        carrerasItems = new ArrayList<SelectItem>();
        asignaturaItems = AsignaturaDAO.getAllAsignaturas();
        pensum = new Pensum(new PensumPK());
        success = false;

        createSelectItems();
    }

    public Pensum getPensum() {
        return pensum;
    }

    public void setPensum(Pensum pensum) {
        this.pensum = pensum;
    }

    public PensumXML getPensumXml() {
        return pensumXml;
    }

    public void setPensumXml(PensumXML pensumXml) {
        this.pensumXml = pensumXml;
    }

    public Carrera getCarrera() {
        return carrera;
    }

    public void setCarrera(Carrera carrera) {
        this.carrera = carrera;
    }

    public List<Asignatura> getAsignaturaItems() {
        return asignaturaItems;
    }

    public void setAsignaturaItems(List<Asignatura> asignaturaItems) {
        this.asignaturaItems = asignaturaItems;
    }

    public List<SelectItem> getCarrerasItems() {
        return carrerasItems;
    }

    public void setCarrerasItems(List<SelectItem> carrerasItems) {
        this.carrerasItems = carrerasItems;
    }

    public String insertar(ActionEvent event) throws IOException {
        
        pensum.setFecha(new Date());
        pensum.setPensum(pensumXml.getXML());

        if (PensumDAO.insertar(pensum)) {
            success = true;
            pensum = new Pensum(new PensumPK());
        }

        return "";
    }

    public void agregarTrimestre(ActionEvent event) {
        int size = pensumXml.getListTrimestres().size();
        
        if (!pensumXml.getListTrimestres().get(size - 1).getListAsignaturas().isEmpty()) {
            TrimestreXML trimestreXml = new TrimestreXML();
            trimestreXml.setListAsignaturas(new ArrayList<AsignaturaXML>());
            pensumXml.getListTrimestres().add(trimestreXml);
        }
    }

    public void agregarAsignatura(ActionEvent event) {
        int size = pensumXml.getListTrimestres().size();

        for (int i = 0; i < asignaturaItems.size(); i++) {
            Asignatura a = asignaturaItems.get(i);

            if (a.isSelected()) {
                AsignaturaXML asignaturaXml = new AsignaturaXML(a.getIdAsignatura(), a.getDescripcion(), new Integer(a.getCreditos()).toString());
                pensumXml.getListTrimestres().get(size - 1).getListAsignaturas().add(asignaturaXml);
                a.setSelected(false);
            }
        }
    }
    
    private void createSelectItems() {
        Iterator itr = CarreraDAO.getAllCarreras().iterator();

        while (itr.hasNext()) {
            Carrera carrera = (Carrera) itr.next();
            carrerasItems.add(new SelectItem(carrera.getIdCarrera(), carrera.getNombre()));
        }
    }

    public void resetearBean(ActionEvent event) {
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        ValueExpression ve = FacesContext.getCurrentInstance().getApplication().getExpressionFactory().createValueExpression(elContext, "#{pensumBean}", ManejoPensumBean.class);

        ManejoPensumBean bean = new ManejoPensumBean();
        ve.setValue(elContext, bean);
    }
    
}